const WXAPI = require('../../wxapi/main')

Page({
    data: {
        fl: {
            pic: '',
            name: '',
            childs: []
        },
        allfl: [],
        ejfl: [],
        list: [],
        id: '',
        page: 1,
        pageSize: 20,
        totalRow: 0,
        totalPage: 0,
        isLoading: false,    // 是否正在加载
        hasMore: true,       // 是否还有更多数据
        scrollTop: 0,
        showUp: false,
        index4: 'https://dcdn.it120.cc/2025/07/04/bc267f57-1c70-4231-a9b0-4e55c97d20eb.jpg'
    },
    // 显示运行
onShow (options) {
     console.log(11111);
        
        const that = this;
       that.setData({
        list:[],
            page:1,
             isLoading: false, 
        hasMore: true,      
        });
        that.getlist();
  },

  
    getlist() {
        const that = this;

        // 如果正在加载或没有更多数据，直接返回
        if (that.data.isLoading || !that.data.hasMore) return;

        that.setData({ isLoading: true });

        const params = {
            page: that.data.page,
            pageSize: that.data.pageSize,
            type: 0,
            token:wx.getStorageSync('token')
        };

        WXAPI.goodsFavListV2(params).then(res => {
            console.log("接口返回：", res);

            if (res.code === 0) {
                let newList = res.data.result || [];

                // 合并旧列表与新数据
                const updatedList = [...that.data.list, ...newList];

                // 判断是否还有更多页
                const hasMore = that.data.page < res.data.totalPage;

                that.setData({
                    list: updatedList,
                    page: that.data.page + 1,
                    totalPage: res.data.totalPage,
                    totalRow: res.data.totalRow,
                    isLoading: false,
                    hasMore: hasMore
                });

                console.log("当前列表长度：", updatedList.length);
            } else {
                // 接口返回异常时也关闭加载状态
                that.setData({ isLoading: false });
                // wx.showToast({ title: '加载失败', icon: 'none' });
            }
        }).catch(err => {
            console.error('加载出错:', err);
            that.setData({ isLoading: false });
            // wx.showToast({ title: '网络错误', icon: 'none' });
        });
    },

    // 页面滚动监听
    onPageScroll(e) {
        const scrollTop = e.scrollTop;

        // 显示/隐藏回到顶部按钮
        this.setData({ scrollTop, showUp: scrollTop > 100 });


        const query = wx.createSelectorQuery();
        query.select('.container').boundingClientRect(res => {
            if (res) {
                const scrollBottom = res.height - (scrollTop);
                if (scrollBottom <= 200) {
                    this.loadMore();
                }
            }
        }).exec();
    },

    // 加载更多方法
    loadMore() {
        if (!this.data.hasMore || this.data.isLoading) {
            return; // 防止重复加载
        }
        this.getlist(); // 调用获取数据的方法
    },

    // 回到顶部
    up() {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 300
        });
    },

    // 分享
    onShareAppMessage() {
        return {
            title: "GOOD STUDIO儿童摄影",
            path: "/pages/index/list?id=" + this.data.id
        };
    },

    onShareTimeline() {
        return {
            title: "GOOD STUDIO儿童摄影"
        };
    },

    // 侧栏功能
    tel() {
        wx.makePhoneCall({
            phoneNumber: wx.getStorageSync('shopSubdetail')?.info?.linkPhone || ''
        });
    },

    sc() {
        wx.navigateTo({
            url: '/pages/index/sc'
        });
    },

    golist(e) {
        wx.navigateTo({
            url: '/pages/index/list?id=' + e.currentTarget.dataset.id
        });
    },

    goxq(e){
          wx.navigateTo({
            url: '/pages/index/detail?id=' + e.currentTarget.dataset.id
        });
    }
});